package pers.vic.practice.module.index.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import pers.vic.boot.base.model.BaseResponse;
import pers.vic.boot.base.model.Remind;
import pers.vic.boot.util.CookieUtil;
import pers.vic.practice.base.controller.BaseController;
import pers.vic.practice.base.enums.UserType;
import pers.vic.practice.base.model.Principal;
import pers.vic.practice.module.index.service.IndexService;
import pers.vic.practice.module.statistics.service.StatisticsService;
import pers.vic.practice.module.student.model.SCourse;
import pers.vic.practice.module.student.service.SCourseService;
import pers.vic.practice.module.system.service.SysDictService;

import javax.annotation.Resource;

/**
 * 描述:
 * @author Vic.xu
 * @date 2022-04-11 17:48
 */
@Controller
public class IndexController extends BaseController {


    @Resource
    private IndexService indexService;

    @Resource
    private StatisticsService statisticsService;

    @Resource
    private SysDictService sysDictService;

    @Resource
    private SCourseService sCourseService;

    @GetMapping(value = {"", "/"})
    public String index(){
        return "index";
    }

    @GetMapping("/login")
    public String login(){
        Principal user = indexService.ifLogin(request);
        if (user != null) {
            storeUser(user);
            return "redirect:/";
        }
        return "login";
    }

    @PostMapping(value = "/login")
    public String login(UserType userType, String username, String password, @RequestParam(required = false) boolean rememberMe,
                        RedirectAttributes attributes) {
        BaseResponse<Principal> response = indexService.login(userType, username, password);
        if (response.getCode() != 0) {
            attributes.addFlashAttribute(Remind.warning().setMessage(response.getMsg()));
            return "redirect:login";
        }
        Principal user = response.getData();
        storeUser(user);
        String userToken = indexService.makeToken(user);
        if (rememberMe) {
            CookieUtil.set(this.response, PRINCIPAL, userToken, rememberMe);
        }
        attributes.addFlashAttribute(Remind.success());
        return "redirect:/";
    }

    /**
     * 首页
     *
     */
    @GetMapping(value = "index")
    public String index(Model model) {
        return "index";
    }

    @GetMapping("/main")
    public String main(Model model){
        model.addAttribute("survey", statisticsService.survey());
        model.addAttribute("terms",  sysDictService.findByTypeCode("term"));
        model.addAttribute("courses", sCourseService.list(new SCourse())) ;

        return "main";
    }

    /**
     * 退出
     *
     * @return
     */
    @GetMapping(value = "logout")
    public String logout() {
        cleanSession();
        CookieUtil.remove(request, response, PRINCIPAL);
        return "redirect:login";
    }
}
